<?php

class Chart extends Controller {
	
	function __construct(){
        parent::__construct();
        $this->load->model("models");
		$this->load->model("version");
		$this->load->model("complete");
	    $this->load->model("category");
	    $this->load->model("company");
	    $this->load->model("receive");
	   // $this->load->model("receive_update");
	    $this->load->model("send");
	    $this->load->model("update_historys");
	    $this->load->model("inventory");
	    $this->load->model("historys");
	}
	
	function index(){
	   $this->top();
	   $mods = $this->models->select('');
	   $data = array(    
	                  'modss'          =>$this->mod_line(),
                    );      
	   $this->load->view('chart_total',$data);
	}
	
	function send_total(){
	   $this->top();
	   $mods = $this->models->select('');
	   $data = array(  
	                  'modss'          =>$this->send_line(),
                     ); 
	   $this->load->view('send_total',$data);	
	}
	
	function update_total(){
	   $this->top();
	   $mods = $this->models->select('');
	   $data = array( 
	                   'modes'      => $this->update_line(),
                      );  
	   $this->load->view('update_total',$data);	  
	}
	
	function inventory_total(){
		$mod_count = $this->inventory->select_distinct_mod();
		$m_names="";

		foreach ($mod_count as $key=>$val){
			$m = $this->models->select($val->mod_id);
			$m_names.= $m[0]->name.";";
		}
		$m_names = rtrim($m_names,';');
		
		$category_count = $this->inventory->select_distinct_category();

		$data['category_count'] = count($category_count);
		$total_count = $this->inventory->select_distinct_count();
		$data['total_count'] = $total_count[0]->count;
		$data['mod_count'] = $m_names;
		
		$this->top();
		$this->load->view('inventory_total',$data);		
	}
	
	
	function top(){
		$user = $this->session->userdata('user');

		if($user!=""){
			$data['user'] = $user;
		}else{ 
			//直接跳转到登录页面
			redirect('main/login');exit;
		}
	    $data['module']=$this->uri->segment(1);
	    $data['template']=$this->uri->segment(2);
	    $data['level_data'] = array('机密','秘密','内部');
	    $data['update_data'] = array('库存已更新','库存未更新','发放已更新','发放未更新');
	    $data['title']="资料统计-用户资料管理系统";
	    $this->load->view('top',$data);
	}
	
	function mod_line(){
	   
        $mods = $this->models->select('');
        $name = array();
        $arr1 = array();
        $arr2 = array();
        foreach($mods as $k=>$val){
        	$name[] = $val->name;
   //取得查询技术资料份数
            $da = "category.mod_id=".$val->mod_id;
            $categorys = $this->category->main_select($da);
	        $total_count = 0;     
	        $co_count = 0;  
	        for($i=0;$i<count($categorys);$i++){
	//查询数量,包括已发放的，和库存的        	
	        	$inven = $this->inventory->getcount($categorys[$i]->category_id,''); 
	        	$co = $this->send->getcount($categorys[$i]->category_id,'');
	        	$total_count+=$inven[0]->count;
			    $total_count+=$co[0]->count;
			    $co_count += $inven[0]->count;
	        }
	        $cate = $this->category->main_select($da);
	        $arr1[] = count($cate);
	        $arr2[] = $total_count;
        }
        return array($name,$arr1,$arr2);
	}
	
 
	
	
	function update_line(){
        $mods = $this->models->select('');
        $name = array();
        $arr1 = array();
        $arr2 = array();
        foreach($mods as $k=>$val){
        $sr = $this->version->get_lastverison(array('mod_id'=>$val->mod_id)); 
        $check_array=array();
		$check_arrays=array();
        $invalid_file=0;
        $invalid_inventory=0;
        $cates = $this->category->select(array('mod_id'=>$val->mod_id));
        foreach ($cates as $k=>$va){
          $to_category[]=$va->category_id;
        }
        $total_counts = 0;       
        for($i=0;$i<count($cates);$i++){
//查询数量,包括已发放的，和库存的        	
        	$inven = $this->inventory->getcount($cates[$i]->category_id,''); 
        	$co = $this->send->getcount($cates[$i]->category_id,'');
        	$total_counts+=$inven[0]->count;
		    $total_counts+=$co[0]->count;
        }
        for($j=0;$j<count($sr);$j++){
	 //先根据种类号，去查询是不是存在，然后再去拿最高版本去查询
                         $cat = $this->send->select_by_array(array('category_id'=>$sr[$j]['category_id']));	
                         $cat_count = count($cat);
                         if($cat_count>0){
                           $re = $this->send->getlast_foo($sr[$j]['version_id'],$sr[$j]['category_id']);
                           if((count($re)!=0)&&(in_array($sr[$j]['category_id'],$to_category))&&(!in_array($sr[$j]['category_id'],$check_array))){                           	
                           	 $check_array[]=$sr[$j]['category_id'];
                           	 $ss = $this->send->select_where($sr[$j]['category_id'],$sr[$j]['version_id']);
                           	// echo $ss.'-----'.$sr[$j]['category_id'].'-------'.$sr[$j]['version_id']."<br>";
                           	 if($ss!=0){
                           	 	$cate_invalid[] = $sr[$j]['category_id'];
                           	 	$mod_invalid[] =  $sr[$j]['mod_id'];
                           	 	$cate_invalid_inventory[] = $sr[$j]['category_id'];
                           	 	$mod_invalid_inventory[] =  $sr[$j]['mod_id'];
                           	 }
                           	 $invalid_inventory+=$ss;
                           	 $invalid_file+=$ss;
                           }
                         }
                        
                         $cat = $this->inventory->select_by_array(array('category_id'=>$sr[$j]['category_id']));	
                         $cat_count = count($cat);
                         if($cat_count>0){
                           $re = $this->inventory->getlast_foo($sr[$j]['version_id'],$sr[$j]['category_id']);
                           if((count($re)!=0)&&(in_array($sr[$j]['category_id'],$to_category))&&(!in_array($sr[$j]['category_id'],$check_arrays))){
                           	 $check_arrays[]=$sr[$j]['category_id'];
                           	 $ss = $this->inventory->select_where($sr[$j]['category_id'],$sr[$j]['version_id']);
                           //	 echo $ss.'-----'.$sr[$j]['category_id'].'-------'.$sr[$j]['version_id']."<br>";
                           	 if($ss!=0){
                           	 	$cate_invalid[] = $sr[$j]['category_id'];
                           	 	$mod_invalid[] =  $sr[$j]['mod_id'];
                           	 }
                           	 $invalid_file+=$ss;
                     }
              } 
             $arr_total[$sr[$j]['mod_id']]=$invalid_file;                          
	    }
	    
        	$name[] = $val->name;
   //取得查询技术资料份数
            $da = "send.mod_id=".$val->mod_id;
            $categorys = $this->send->main_select($da);
	        $total_count = 0;     
	        $co_count = 0;  
	        for($i=0;$i<count($categorys);$i++){
	//查询数量,包括已发放的，和库存的        	
	        	$co = $this->send->getcount($categorys[$i]->category_id,'');
			    $total_count+=$co[0]->count;
	        }
	       
	        $cate = $this->send->main_select($da);
	       
	        $arr1[] = $total_counts-$invalid_file;
	        $arr2[] = $invalid_file;
        }	
        $mod = array_unique($mod_invalid);
        $m_names="";
        foreach ($mod as $key=>$val){
        	$m = $this->models->select($val);
        	$m_names.= $m[0]->name.";";
        }
        $m_names = rtrim($m_names,';');
        $cate = count(array_unique($cate_invalid));
        $mod_inventory = count(array_unique($mod_invalid_inventory));
        $cate_inventory = count(array_unique($cate_invalid_inventory));
        return array('valid'=>$arr1[0],'invalid'=>array_sum($arr_total),'mod_count'=>$m_names,'cate_count'=>$cate,'mod_count_inventory'=>$mod_inventory,'cate_count_inventory'=>$cate_inventory,'invalid_inventory'=>$invalid_inventory,);	
	}
	

	
	
 function send_line(){
   //先取公司，再去型号，种类，数量   
            $send_data = $this->send->main_select_com('1=1');

            foreach ($send_data as $key=>$val){
            	$mods = $this->send->main_select_mod('send.company_id = '.$val->company_id);
            	if(count($mods)!=0){
	            	foreach ($mods as $k=>$v){
	            		$categorys = $this->send->main_select_charts('send.company_id = '.$val->company_id.' and send.mod_id='.$v->mod_id);     
	            		                 
	            		foreach ($categorys as $i=>$j){      
	            		   $com = $this->company->select($j->company_id);   	
	            		   $mo = $this->models->select($j->mod_id);
	            		   $c = $this->category->select(array('category_id'=>$j->category_id));
	                       $m[$com[0]->name][$mo[0]->name]['category_name'][] =$c[0]->name;
	                       $m[$com[0]->name][$mo[0]->name]['send_c'][] =$j->count;
	            		}
	            	}      
            	}      		
            }
  
         foreach ( $m as $key=>$val){
         	 
         	$a = 0;
         	foreach ($val as $k=>$v){
         		$mod_count =  $k;
         		$cate_name = array_unique($v['category_name']);
         		$category_count = count($cate_name);
         		
         		foreach($v['send_c'] as $i=>$j){
         			$a+=$j;
         		}
         	}
         	$coms[$key]['mod_count'] = $mod_count;
         	$coms[$key]['cate_count'] = $category_count;
         	$coms[$key]['counts'] = $a;
         }
        return $coms;
	}
	

}	